package com.css.bjdt_core.reportPrepare.dao;

import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.css.bjdt_core.reportPrepare.entity.RptLssuedAreasInfo;
import com.css.bjdt_core.reportPrepare.entity.RptLssuedAreasTemplateRel;
import com.css.bjdt_core.util.PageQuerys;

@Repository
public class RptLssuedAreasInfoDao {

	@Autowired
	private Dao dao;

	public void saveARptLssuedAreasInfo(RptLssuedAreasInfo rptLssuedAreasInfo) {
		dao.insert(rptLssuedAreasInfo);
	}

	public RptLssuedAreasInfo getARptLssuedAreasInfoById(String areaId) {
		return dao.fetch(RptLssuedAreasInfo.class, Cnd.where("AREA_ID", "=", areaId));
	}

	public void deleteARptLssuedAreasInfoById(String areaId) {
		dao.clear(RptLssuedAreasInfo.class, Cnd.where("AREA_ID", "=", areaId));
		dao.clear(RptLssuedAreasTemplateRel.class, Cnd.where("AREA_ID", "=", areaId));
	}

	public void saveRptLssuedAreasTemplateRelList(List<RptLssuedAreasTemplateRel> rptLssuedAreasTemplateRelList,
			String areaId) {
		dao.clear(RptLssuedAreasTemplateRel.class, Cnd.where("AREA_ID", "=", areaId));
		dao.insert(rptLssuedAreasTemplateRelList);
	}

	public void updateARptLssuedAreasInfo(String areaName, String areaId) {
		String strSql = "update RPT_LSSUED_AREAS_INFO set AREA_NAME = @areaName where AREA_ID = @areaId";
		Sql sql = Sqls.create(strSql);
		sql.params().set("areaName", areaName).set("areaId", areaId);
		dao.execute(sql);
	}

	/**
	 * 分页查询
	 * @param cnd
	 * @return
	 */
	public Map queryPage(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, RptLssuedAreasInfo.class, cnd, pageNum, pageSize);
		return result;
	}

	/**
	 * 查询当前区域下包含多少报表
	 * @param areaId
	 * @return
	 */

	public List<RptLssuedAreasTemplateRel> getAllTemplateIdByAreaId(String areaId) {
		return dao.query(RptLssuedAreasTemplateRel.class, Cnd.where("AREA_ID", "=", areaId));
	}
}
